<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetPromotionRegionGoods.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\goods;

use app\api\model\dao\Goods;
use app\api\model\dao\PromotionRegion;
use app\api\model\dao\PromotionRegionGoods;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetPromotionRegionGoods extends PageBase
{

    private $objPromotionRegionGoods;
    private $objPromotionRegion;
    private $objGoods;

    public function __construct()
    {
        parent::__construct();
        $this->objPromotionRegionGoods = new PromotionRegionGoods();
        $this->objPromotionRegion = new PromotionRegion();
        $this->objGoods = new Goods();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'rid' => 'require',
        ];
        $msg = [
            'rid.require' => '商品活动专区ID必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {

        // TODO:业务逻辑
        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $rid = isset($param['rid']) ? intval($param['rid']) : 0;

        $rets = $this->objPromotionRegionGoods->getPromotionRegionGoods($rid);
        if (false === $rets) {
            return $this->getRes(Error::ERR_SYS);
        }

        $promotion_region = $this->objPromotionRegion->getPromotionRegion($rid);

        $goods_arr = array();
        if (!empty($rets)) {
            $goods_ids = array_map(function ($v) {
                return $v['goods_id'];
            }, $rets);
            $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
            foreach ($goods as $good) {
                $goods_arr[$good['goods_id']] = $good;
            }
            foreach ($rets as &$ret) {
                if (isset($goods_arr[$ret['goods_id']])) {
                    $ret['market_price'] = $goods_arr[$ret['goods_id']]['market_price'];
                    $ret['shop_price'] = $goods_arr[$ret['goods_id']]['shop_price'];
                    $ret['sale_count'] = $goods_arr[$ret['goods_id']]['sale_count'];
                    // 加上折扣
                    $ret['discount'] = '' . round($goods_arr[$ret['goods_id']]['shop_price'] / $goods_arr[$ret['goods_id']]['market_price'], 2) * 10;
                    $ret['goods_thumb'] = $goods_arr[$ret['goods_id']]['goods_thumb'];
                }
            }
        }

        $this->data = array(
            "rid" => $promotion_region[0]['rid'],
            "name" => $promotion_region[0]['name'],
            "title" => $promotion_region[0]['title'],
            "thumb" => $promotion_region[0]['thumb'],
            "start_time" => $promotion_region[0]['start_time'],
            "end_time" => $promotion_region[0]['end_time'],
            "list" => $rets,
        );
        return $this->getRes();
    }
}
